<?php
namespace app\admin\model;

/*客服模型*/
use app\admin\model\CommonModel;
use think\Db;

class Config extends CommonModel
{
    private $table_name = 'config';
    public function __construct()
    {
       parent::__construct();
    }
    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        $default = [
            "cKey"     => "",
            "cValue" => "",
            "type"      => "text",
        ];
        return array_merge($default,$args);
    }

    /**新增一个配置
     * @param $args [cKey,cValue,type]
     * @return client_id
     */
    public function add($args)
    {
        //初始化参数
        $args = $this->default_param($args);

        if(empty($args['cKey']))
        {
            return $this->returnState(1,"配置项填写不完整");
        }

        //查询是否已存在
        $info = $this->info(['cKey'=>$args['cKey']]);
        if($info === false || !empty($info))
        {
            return $this->returnState(1,"配置项已存在，或者配置项信息不完整");
        }

        //插入数据库
        db($this->table_name)->insertGetId([
            'cKey'       => $args['cKey'],
            'cValue'      => $args['cValue'],
            'type'          => $args['type']
        ]);

        //返回成功标识
        return $this->returnState(0,"成功添加配置项，请开发人员到admin/config/index.html添加'".$args['type']."'项");
    }

    //获取所有自动回复
    public function all()
    {
        $data = db($this->table_name)->select();
        $arr = array();
        if(!empty($data))
        {
            foreach($data as $v)
            {
                $arr[$v['cKey']] = $v['cValue'];
            }
        }
        //这里做些初始化配置的操作
        return $arr;
    }

    /**获取一个自动回复记录
     * @param array $args key:[id,cKey], 中括号表示任选一个可以查询
     * @return bool
     */
    public function info($args)
    {

        if(empty($args)) return false;
        //参数合并
        $args = $this->default_param($args);
        //如果没有查询信息，返回空
        if((!isset($args['id']) || empty($args['id'] ))
            && empty($args['cKey'] )
        ) return false;

        //根据id,cKey其中一个查询
        if(!empty($args['id']))
        {
            $filter = ['id'=>$args['id']];
        }
        elseif(!empty($args['cKey']))
        {
            $filter = ['cKey'=>$args['cKey']];
        }
        if(empty($filter)) return false;

        //返回查询信息
        return  db($this->table_name)->where($filter)->find();
    }

    //删除自动回复
    public function del($args)
    {
        /**
         * args参数说明
         * args为一个数组，包含所有的id值
         */
        if(empty($args))
        {
            return $this->returnState(1,"没有记录被删除");
        }
        db($this->table_name)->where(['id'=>['in',implode(',',$args)]])->delete();
        return $this->returnState(0,"删除完毕");
    }

    //编辑一个记录
    public function edit($args)
    {
        //检查记录是否存在
        $list = $this->all();
        if(!$list || empty($list))
        {
            return false;
        }
        //把cKey组合一个数组
        $cKeyList = array_keys($list);
        //过滤输入的空值
        $args = array_filter($args,function($var){
            return $var!=0 || !empty($var);
        });
        $cKey = [];
        $when = "";
        if(!empty($args))
        {
            foreach($args as $k=>$v)
            {
                if(in_array($k,$cKeyList))
                {
                    //删除旧图片
                    @unlink("./../".$list[$k]);
                    $cKey[] = "'$k'";
                    //删除数据库没有的项
                    $when.="  WHEN '$k' THEN '$v' ";
                }
                else
                {
                    unset($args[$k]);
                }
            }
        }
        if(empty($cKey)) return false;
        Db::query("
            UPDATE ecs_config
                  SET cValue = CASE cKey
                  $when
                  END
            WHERE cKey in(".implode($cKey,",").")
        ");

    }

}
